<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once "../_inc/procFunction.php";

$errMsg = sxREQ('msg');
$module = sxREQ('mod');
$subModule = sxREQ('sub');
if($module==''){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("var. 'mod' missing")); 
	serverRedir($url);
}
if($subModule==''){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("var. 'sub' missing")); 
	serverRedir($url);
}
if(sxREQ('del')=='yes'){
	$_REQUEST["module"] = $module;
	$_REQUEST["sub_module"] = $subModule;
	$errMsg .= subModDel();
}
$sSQL = "SELECT sm.*, m.description AS mdes, fa.description AS fades " .
			" FROM sub_module sm " .
			" INNER JOIN module m ON m.module = sm.module " .
			" INNER JOIN functional_area fa ON fa.module = sm.module " .
			"    AND fa.functional_area = sm.functional_area " .
			" WHERE sm.module = '".$module."' " .
			" 	AND sm.sub_module = '".$subModule."' ";
$objSDAO->query($sSQL, "mod");
if(!$row = $objSDAO->fetch("mod")){
	$url = $_SESSION[USEROBJ]->getHistory(-1, true).
		"&msg=".urlencode(_t("sub-module '%s' missing",$module.'.'.$subModule)); 
	serverRedir($url);
}

$sSQL = "SELECT pu.*
		FROM process_unit pu 
		WHERE pu.module='$module' 
		AND pu.sub_module='$subModule' 
		ORDER BY process_unit";

$objSDAO->query($sSQL, "pu");

$aOpz = array("kMenu"=>'pro.mod', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script> 
<script language="JavaScript" type="text/javascript">
  function confDelSubMod() {
  		if (confirm("Confirm ?")) {
  			document.location.href = "subModView.php?del=yes<?="&mod=$module&sub=$subModule&xsn=".XSN?>";
  		}
  }
</script>
  

<?php
echo(bodyHTML($aOpz));

echo getFaHeader($module, $row['functional_area']);

$aCmd = array(	
					"afun"=>array("pag"=>"../modl/functionalAreaIns.php?mod=$module",
										"title"=>_t("Add new functional-area"),
										"text"=>_t("new functional-area"),
										"perm"=>"viewmod",
										"align"=>"L"),
					"newpro"=>array("pag"=>"../unit/puIns.php?mod=$module&sub=$subModule",
										"title"=>_t("add process-unit"),
										"text"=>_t("new process-unit"),
										'align'=>'R'),
					"mod"=>array("pag"=>"subModMod.php?mod=$module&sub=$subModule",
										"title"=>_t("change sub-module"),
										"text"=>_t("modifiy"),
										"perm"=>"viewmod"),
					"del"=>array("pag"=>"javascript:confDelSubMod();",
										"title"=>_t("delete sub-module"),
										"text"=>_t("delete"),
										"perm"=>"viewmod"),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1)
										)
					);
echo(commandsHTML($aCmd));	
echo("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>"._t("Sub-module")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">"._t("Name")."</td>" .
			"<td class=\"text01\" width=\"80%\">$subModule</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"20%\">"._t("Description")."</td>" .
			"<td class=\"text01\" width=\"80%\">" .$row['description'].
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\"><pre class=\"text01\">".sxWW($row['note'])."</pre></td>" .
		"</tr>\n");
echo("</table>\n");

echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
		"<td class=\"titl01\" colspan=\"5\">"._t("List of Process-units")."</td>" .
	"</tr>\n");
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\">&nbsp;</td>" .
		"<td class=\"tabh01\" width=\"14%\">"._t('Process-unit')."</td>" .
		"<td class=\"tabh01\" width=\"54%\">"._t('Description')."</td>" .
		"<td class=\"tabh01\" width=\"20%\">"._t('Type')."</td>" .
		"<td class=\"tabh01\" width=\"1%\">"._t('Export')."</td>" .
	"</tr>\n");	
$numElem = 0;
while($row = $objSDAO->fetch("pu")) {
		$numElem ++;
		echo("<tr>" .
				"<td class=\"text01\" valign='top'><a class=\"text01\" ".
					goToPag("../entp/entpList.php?pu=".$row["process_unit"]."&xsn=".XSN).
					" title=\""._t("Lista punti di ingresso")." '".sxHE($row["description"])."'\">".
					"<img src='".SERVER_URL_ROOT."/app/_img/b_engine.png' border='0'></a></td>\n" .
				"<td class=\"text01\" valign='top'><a class=\"text01\" ".
					goToPag("../unit/puView.php?pu=".$row["process_unit"]."&xsn=".XSN).
					" title=\""._t("View process-unit")." '".sxHE($row["description"])."'\">".
					$row["process_unit"]."</a></td>\n" .
				"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
				"<td class=\"text01\" valign='top'>".sxHE($row["type"])."</td>" .
				"<td class=\"text01\" valign='top' align=\"center\">" .
				"<input type=\"checkbox\" name=\"exp_".$numElem."\" id=\"exp_".$numElem.
				"\" value=\"".$row["process_unit"]."\" class='text01'></td>" .
			"</tr>\n");
}
echo("</table>\n");

echo(commandsHTML($aCmd));
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function subModDel() {
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["module"]) || $_REQUEST["module"] == "")
		$errMsg .= _t("module name missing");
	if(!isset($_REQUEST["sub_module"]) || $_REQUEST["sub_module"] == "")
		$errMsg .= _t("sub-module name missing");
	$module = $_REQUEST["module"];
	//verifico che la data_area non sia usata
	$sSQL = "SELECT count(*) AS num FROM process_unit WHERE module='".$_REQUEST["module"]."' AND sub_module='".$_REQUEST["sub_module"]."'";
	if($objSDAO->query($sSQL,"tst",array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("tst")){
			$num = $row["num"];
			if($num >0){
				$errMsg .= _t("existing process - no delete")."<br>";
			}
		}
	}else{
		$errMsg .= $sSQL."<br> - ".$objSDAO->error;
	}
	
	if($errMsg == ""){
		$sSQL = "DELETE FROM sub_module " .
				"WHERE module='".$_REQUEST["module"]."' AND sub_module='".$_REQUEST["sub_module"]."'";	
		if($objSDAO->query($sSQL,"del",array("contOnErr"=>true))){
			$_SESSION[USEROBJ]->reLoad();
			logRecord("500", _t("Eliminato sotto-modulo '".addslashes(sxGPC(strtolower($_REQUEST["sub_module"])))."' del modulo '".addslashes(sxGPC(strtolower($_REQUEST["module"])))."'"));
			//serverRedir("subModList.php?mod=".$_REQUEST["module"]."&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}
